package dht.data;

import java.util.Vector;

import dht.simulation.RouteTableV2;
import dht.simulation.VNodeV2;

public class Message implements Comparable<Message>, Cloneable {
	public int accessId;
	public MessageType msgType;
	public int fromId;
	public int toId;
	public double generateTime;
	public double waitingCost;
	public double processCost;
	public double sendTime;
	public double networkCost;
	public int msgSize;// in bytes

	// other attributes depend on msgType
	public int fwdToId;
	public int moveToId;
	public int moveSourceId;
	public RouteTableV2 table;
	public Vector<VNodeV2> vecVnodes;

	public Message() {
		accessId = 0;
		fromId = 0;
		toId = 0;
		generateTime = 0.0;
		waitingCost = 0.0;
		processCost = 0.0;
		sendTime = 0.0;
		networkCost = 0.0;
		msgSize = 0;
		msgType = null;
		fwdToId = 0;
		moveToId = 0;
		moveSourceId = 0;
		table = null;
		vecVnodes = null;
	}

	public String toString() {
		return "###Message begin###\naccessId: " + accessId + " msgType: "
				+ msgType + " fromId: " + fromId + " toId: " + toId
				+ " generateTime: " + generateTime + " waitingCost: "
				+ waitingCost + " processCost: " + processCost + " sendTime: "
				+ sendTime + " networkCost: " + networkCost + " msgSize: "
				+ msgSize + "\n###Message end###\n";
	}

	public double getCurrentTime() {
		return generateTime + waitingCost + processCost + networkCost;
	}

	@Override
	public int compareTo(Message o) {
		double l = getCurrentTime();
		double r = o.getCurrentTime();
		if (l < r)
			return -1;
		else if (l > r)
			return 1;
		return 0;
	}

	public synchronized Object clone() {
		try {
			Message m = (Message) super.clone();
			if (table != null) {
				m.table = (RouteTableV2) table.clone();
			}
			if (vecVnodes != null) {
				m.vecVnodes = new Vector<VNodeV2>();
				for (VNodeV2 vnode : vecVnodes) {
					m.vecVnodes.add(vnode);
				}
			}
			return m;
		} catch (CloneNotSupportedException e) {
			throw new InternalError();
		}
	}
}
